﻿$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};

jQuery.fn.listPager = function (options) {

    var settings = jQuery.extend({
        api: false,
        keyName: "listPagerApi"        
    }, options);

    var pagerApi = $(this).data(settings.keyName);

    if (!pagerApi)
        pagerApi = new listPager($(this), options);

    $(this).data(settings.keyName, pagerApi);

    if (settings.api)
        return pagerApi;
    else
        return $(this);

}

var listPager = function (listCtrl, options) {

    var api = this;
    var wrapper;
    var paging;
    var interval;

    var settings = jQuery.extend({
        pageSize: 1,
        rotate: true,
        interval: 4000,        
        pagerClass: "listpager",
        pagerSelector: ".listpager",
        pagerPagingClass: "paging-controls",
        pagerPagingSelector: ".paging-controls",
        pagerPagingBackClass: "paging-back",
        pagerPagingBackSelector: ".paging-back",
        pagerPagingBackText: "Back",
        pagerPagingNextClass: "paging-next",
        pagerPagingNextSelector: ".paging-next",
        pagerPagingNextText: "Next",
        pagerPagingCountClass: "paging-count",
        pagerPagingCountSelector: ".paging-count",
        displayed: function () {
            api.setCountText((api.index + 1) + " of " + api.count());
        }
    }, options);

    this.index = 0;

    var init = function () {

        wrapper = createWrapper();
        paging = createPaging();

        api.display(0);

        startInterval();

    };

    var startInterval = function () {
        interval = setInterval(function () {
            api.next();
        }, settings.interval);
    };

    var createPaging = function () {
        wrapper.append("<p class=\"" + settings.pagerPagingClass + "\"><a class=\"" + settings.pagerPagingBackClass + "\" href=\"javascript:void(0)\">" + settings.pagerPagingBackText + "</a><span class=\"" + settings.pagerPagingCountClass + "\"></span><a class=\"" + settings.pagerPagingNextClass + "\" href=\"javascript:void(0)\">" + settings.pagerPagingNextText + "</a></p>");
        return $(settings.pagerPagingSelector);
    };

    var createWrapper = function () {
        listCtrl.wrap("<div class=\"" + settings.pagerClass + "\"></div>");
        return $(settings.pagerSelector).has(listCtrl);
    };

    var resetInterval = function () {
        clearInterval(interval);
        startInterval();
    };

    this.setCountText = function (value) {
        paging.find(settings.pagerPagingCountSelector).text(value);
    };

    this.display = function (index) {

        this.index = index;

        listCtrl.find("li").hide().slice(api.index * settings.pageSize, (api.index * settings.pageSize) + settings.pageSize).fadeIn();
        listCtrl.show();

        if (typeof settings.displayed == "function") {
            settings.displayed(api);
        }

    };

    this.count = function () {
        return listCtrl.find("li").length;
    };

    this.next = function () {
        api.display(api.index == (api.count() - 1) ? 0 : ++api.index);
    };

    this.back = function () {
        api.display(api.index == 0 ? api.count() - 1 : --api.index);
    };

    //don't move initializer method or members below it
    init();

    paging.find(settings.pagerPagingBackSelector).live("click", function () {
        api.back();
        resetInterval();
    });

    paging.find(settings.pagerPagingNextSelector).live("click", function () {
        api.next();
        resetInterval();
    });

    listCtrl.find("a").hover(function () {
        clearInterval(interval);
    }, function () {
        startInterval();
    });

}